package Day3_2student;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentManager {
    //学生信息的集合(全局使用）
    static ArrayList<Student> list=new ArrayList<>();
    public static void main(String[] args) {
        boolean isFish=true;
        while (isFish) {
            System.out.println("------------欢迎来到学生管理系统------------");
            System.out.println("1。添加学生" + "\t" + "2.删除学生" + "\t" + "3.修改学生信息" + "\t" + "4.查看学生信息" + "\n" + "5.退出系统");
            Scanner scanner = new Scanner(System.in);
            int number=scanner.nextInt();

            switch (number) {
                case 1:
                    //添加学生
                    System.out.println("请输入学生信息");
                    addstudent();
                    break;
                case 2:
                    //删除学生
                    System.out.println("删除学生信息");
                    deleteStudent();
                    break;
                case 3:
                    //修改学生信息
                    System.out.println();
                    updateStudent();
                    break;
                case 4:
                    //查看学生信息
                    System.out.println("查看学生信息");
                    getStudentInfo();
                    break;
                case 5:
                    //退出系统
                    System.out.println("系统已退出");
                    isFish=false;
                    break;
            }
        }
    }
    private static void addstudent(){
        System.out.println("请输入学号");
        Scanner scanner=new Scanner(System.in);
        String idNumber= scanner.nextLine();

        System.out.println("请输入姓名");
        String name=scanner.nextLine();

        System.out.println("请输入手机号码");
        String phoneNumber=scanner.nextLine();

        System.out.println("请输入年龄");
        int age=scanner.nextInt();

        //创建学生对象
        Student student=new Student(idNumber,name,age,phoneNumber);

        //
        list.add(student);
        System.out.println("添加成功");
    }

    //查看学生
    private static void getStudentInfo(){
        for (int i=0;i<list.size();i++){
            Student student=list.get(i);
            System.out.println(student.getIdNumber()+"\t"+student.getName()+"\t"+student.getAge()+"\t"+student.getPhoneNumber());
        }
    }

    //删除学生信息
    private static void deleteStudent(){
        System.out.println("请输入需删除学生学号");
        Scanner scanner=new Scanner(System.in);
        String idNumber=scanner.nextLine();
        boolean isSuccess=false;

        //通过学号去 list 中寻找 这个学号是否存在
        for (int i=0;i<list.size();i++){
            Student student=list.get(i);
            if (idNumber.equals(student.getIdNumber())){
                //存在这个学号的学生 删除这个学号的学生
                list.remove(i);
                isSuccess=true;
                break;
            }
        }
        if (isSuccess){
            System.out.println("无对应学生");
        }
    }

    //学生信息修改
    private static void updateStudent(){
        System.out.println("请输入需更改学生学号");
        Scanner scanner=new Scanner(System.in);
        String idNumber=scanner.nextLine();

        for (int i=0;i<list.size();i++){
            Student student=list.get(i);
            if (idNumber.equals(student.getIdNumber())){
                //说明学生存在
                System.out.println("请输入学生新的姓名");
                String newName=scanner.nextLine();

                System.out.println("请输入学生新的号码");
                String phoneNew=scanner.nextLine();

                System.out.println("请输入学生新的年龄");
                int ageNew=scanner.nextInt();

                //修改信息
                student.setName(newName);
                student.setAge(ageNew);
                student.setPhoneNumber(phoneNew);

                //修改集合当中的数据
                list.set(i,student);
            }

        }
    }
}
